package com.glory.dgcpcpda.activity.cpc;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.glory.dgcpcpda.R;
import com.glory.dgcpcpda.base.BaseActivity;
import com.glory.dgcpcpda.callback.CustomDialogEntityCallBack;
import com.glory.dgcpcpda.entity.PrintEntity;
import com.glory.dgcpcpda.entity.response.BaseResponseBean;
import com.glory.dgcpcpda.utils.NetUtils;
import com.glory.dgcpcpda.utils.ScaleView;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_EditTextUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import butterknife.BindView;

public class MapPracticeActivity extends BaseActivity {
    @BindView(R.id.tvTitle)
    TextView tvTitle;

    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText editText;

    @BindView(R.id.llMain)
    LinearLayout llMain;

    private ScaleView imageView;
    private MyHandler handler1;
    class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            imageView = findViewById(R.id.scaleView);
            imageView.setImageBitmap((Bitmap)msg.obj);
        }
    }

    public void getData(String lotId){
        FreeUI_EntityCallBack<BaseResponseBean<PrintEntity>> callBack = new CustomDialogEntityCallBack<BaseResponseBean<PrintEntity>>
                (new TypeToken<BaseResponseBean<PrintEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<PrintEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), false, getSupportFragmentManager())) {
                    PrintEntity body = response.body().getResponse().getBody();
                    String code = body.getCode();

                    String[] s = code.split(" ");
                    byte[] bytes = new byte[s.length];

                    for (int i = 0; i < s.length; i++){
                        int i1 = Integer.parseInt(s[i]);
                        byte bys = (byte) i1;
                        bytes[i] = bys;
                    }

                    InputStream is = new ByteArrayInputStream(bytes);
                    Bitmap bm = BitmapFactory.decodeStream(is);
                    Message msg = new Message();
                    msg.obj = bm;
                    handler1.sendMessage(msg);
                }
            }

            @Override
            public void onError(Response<BaseResponseBean<PrintEntity>> response) {
                super.onError(response);

                Bitmap bm = BitmapFactory.decodeStream(null);
                Message msg = new Message();
                msg.obj = bm;
                handler1.sendMessage(msg);

                loadError(response.getException(), "DaemonUpdateType");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ActionName","Query");
        map.addProperty("EqpNo",lotId);
        NetUtils.requestPostNet("CPC.MapSpecification", "MapSpecification", this, map, callBack);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_cpc_mappractise;
    }

    @Override
    protected void initView() {
        tvTitle.setText("Map规范");
        setMainLayout(llMain);

        handler1 = new MyHandler();
        initEdit(editText,s ->getData(editText.getText().toString().trim()) );
    }

    @Override
    protected void initData() {

    }

    protected void initEdit2(EditText editText, final BaseActivity.editTextListener listener){
        editText.setImeOptions(EditorInfo.IME_ACTION_GO);
        editText.setSingleLine(true);
        editText.setOnEditorActionListener((textView, i, keyEvent) -> {
            if (EditorInfo.IME_ACTION_GO == i) {
                String text = editText.getText().toString();
                editText.getText().clear();
                editText.setText(text);
                editText.setSelection(editText.getText().length());
                return true;
            }
            return false;
        });
        FreeApi_EditTextUtils.setEditTextListener(editText, this, s -> {
            if (listener != null){
                listener.onTextChangeListener(s);
            }
        });
    }

}


